package com.live.dengdengtv.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.live.dengdengtv.R;
import com.live.dengdengtv.bean.gson.ConversationCard;
import com.live.dengdengtv.bean.gson.CustomMsg;
import com.live.dengdengtv.utils.DateUtils;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.tencent.TIMCustomElem;
import com.tencent.TIMElem;
import com.tencent.TIMElemType;
import com.tencent.TIMMessage;
import com.tencent.TIMTextElem;

import java.util.Date;
import java.util.List;

/**
 * Created by wyf on 2016/9/22.
 */
public class ConCardAdpter extends BaseAdapter {

    private Context context;
    private List<ConversationCard> conversationList;

    public ConCardAdpter(Context context, List<ConversationCard> conversationList) {
        this.context = context;
        this.conversationList = conversationList;
    }

    @Override
    public int getCount() {
        return conversationList.size();
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;

        if (convertView != null) {
            holder = (ViewHolder)convertView.getTag();
        } else {
            convertView = View.inflate(context, R.layout.card_people_info, null);

            holder = new ViewHolder();
            holder.name_tv = (TextView) convertView.findViewById(R.id.name_tv);
            holder.msg_tv = (TextView) convertView.findViewById(R.id.msg_tv);
            holder.time_tv = (TextView) convertView.findViewById(R.id.time_tv);
            holder.head_iv = (ImageView) convertView.findViewById(R.id.head_iv);
            holder.sex_iv = (ImageView) convertView.findViewById(R.id.sex_iv);
            holder.level_iv = (ImageView) convertView.findViewById(R.id.level_iv);

            convertView.setTag(holder);
        }

        final ConversationCard timMessage = conversationList.get(position);
        long time = timMessage.create_time;

        String userId = timMessage.user_id;
        String nickName = timMessage.nickName;
        String msg = timMessage.lastMsg;
        String faceUrl = timMessage.faceUrl;
        String date = DateUtils.dateToString(new Date(time),"yyyy年M月d日");


        holder.name_tv.setText(TextUtils.isEmpty(nickName)? userId:nickName);
        holder.msg_tv.setText(msg);
        holder.time_tv.setText(date);
        if(!TextUtils.isEmpty(faceUrl)){
            ImageLoader.getInstance().displayImage(faceUrl, holder.head_iv);
        }else {
            holder.head_iv.setImageResource(R.mipmap.head_default);
        }

        return convertView;
    }

    private static class ViewHolder{
        TextView name_tv;
        TextView msg_tv;
        TextView time_tv;
        ImageView head_iv;
        ImageView sex_iv;
        ImageView level_iv;
    }
}
